#!/bin/bash

# 快速部署脚本
echo "开始部署 SmartResume-AI..."

# 安装Docker
if ! command -v docker &> /dev/null; then
    echo "安装Docker..."
    yum update -y
    yum install -y yum-utils
    yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    yum install -y docker-ce docker-ce-cli containerd.io
    systemctl start docker
    systemctl enable docker
fi

# 安装Docker Compose
if ! command -v docker-compose &> /dev/null; then
    echo "安装Docker Compose..."
    curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
fi

# 克隆项目
PROJECT_DIR="/opt/smartresume-ai"
if [ -d "$PROJECT_DIR" ]; then
    cd $PROJECT_DIR
    git pull origin main
else
    git clone https://github.com/Miku-da-you/SmartResume-AI.git $PROJECT_DIR
    cd $PROJECT_DIR
fi

# 配置环境变量
cp ai-server/.env.example ai-server/.env
sed -i 's/your_app_id_here/a00be55e/g' ai-server/.env
sed -i 's/your_api_secret_here/Y2JhMGNiMjU2MTg4NDYyM2EwMzUwNTYy/g' ai-server/.env
sed -i 's/your_api_key_here/128c4b382bbc366eda1e59d222725a15/g' ai-server/.env

# 配置防火墙
if systemctl is-active --quiet firewalld; then
    firewall-cmd --permanent --add-port=80/tcp
    firewall-cmd --permanent --add-port=443/tcp
    firewall-cmd --permanent --add-port=8080/tcp
    firewall-cmd --permanent --add-port=5005/tcp
    firewall-cmd --permanent --add-port=8888/tcp
    firewall-cmd --reload
fi

# 构建和启动服务
docker-compose down
docker-compose build
docker-compose up -d

echo "部署完成！"
echo "访问地址: http://117.72.205.240"
echo "后端API: http://117.72.205.240:8080"
echo "AI服务: http://117.72.205.240:5005"